package com.example.common.utils;

import com.tbruyelle.rxpermissions2.RxPermissions;

import androidx.fragment.app.FragmentActivity;
import io.reactivex.Observable;
import io.reactivex.functions.Consumer;

public class PermisssionUtils {
    private PermisssionUtils(){}
    private volatile static PermisssionUtils instance=null;
    public static PermisssionUtils getInstance(){
        if (null==instance){
            synchronized (PermisssionUtils.class){
                if (null==instance){
                    instance=new PermisssionUtils();
                }
            }
        }
        return instance;
    }

    /**
     * 申请权限
     * @param fragmentActivity
     * @param permisssions
     */
    public void requestPermission(FragmentActivity fragmentActivity,RequestPermissionCallback callback,String... permisssions){
        RxPermissions rxPermissions = new RxPermissions(fragmentActivity);
        Observable<Boolean> request = rxPermissions.request(permisssions);
        request.subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(Boolean result) throws Exception {
                if (result){
                    callback.onSuccess();
                }else{
                    callback.onFailed();
                }
            }
        });


    }

    public interface RequestPermissionCallback{
        void onSuccess();
        void onFailed();
    }
}
